package com.project.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

@RestController
@RequestMapping("cookie")
public class CookieController {

    @RequestMapping("add")
    public String addCookie(HttpServletResponse resp){
        //创建cookie对象
        Cookie c1 = new Cookie("name","tom");
        Cookie c2 = new Cookie("userId","23");
        //设置过期时间
        c2.setMaxAge(60);

        //向客户端发送cookie信息
        resp.addCookie(c1);
        resp.addCookie(c2);

        return "ok";
    }

    @RequestMapping("getCookie")
    public String getCookie(HttpServletRequest req){
        //得到客户端提交的Cookie信息
        Cookie[] cookieArray = req.getCookies();
        String str = "";
        for(Cookie c : cookieArray){
            str += "  键："+c.getName()+"  值："+c.getValue();
        }

        return str;
    }
}
